﻿{% extends "base.html" %}



{% block css %}
<link rel="stylesheet" href="/static/css/admin.css">
<link rel="stylesheet" href="/static/ztree3/css/bootstrapStyle/bootstrapStyle.css" type="text/css">
{% endblock %}


{% block center %}



<!--用户信息-->
<div class="panel panel-default center-top" style="min-width:1300px;">
  <div class="panel-heading">
      <div class="row" style="padding-top:5px;padding-left:10px;">
           <div class="col-sm-2">

                <ol class="breadcrumb" style="margin-bottom: 0px;">
                  <li><a href="/" style="color: #377CD8;">首页</a></li>
                  <li><a href="javascript:;">系统管理</a></li>
                  <li class="active">{{ title }}</li>
                </ol>

          </div>


      </div>

  </div>
  <div class="panel-body">

        <div class="row">
            <div class="col-sm-2" style="max-height:735px;overflow:auto;">

                <ul id="treeDemo" class="ztree"></ul>

            </div>


            <div class="col-sm-10" id="dir-list">
                <ul class="nav nav-tabs" id="nav-change">
                  <li role="presentation" class="active"><a href="javascript:;">执行命令</a></li>
                  <li role="presentation" ><a href="javascript:;">文件管理</a></li>
                  <li role="presentation"><a href="javascript:;">执行脚本</a></li>
                  <li role="presentation"><a href="javascript:;">计划任务</a></li>
                </ul>

                <!--批量执行命令-->
                <div class="row" style="margin-left:0px;margin-top:20px;" id="div-cmd">
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="input-group">
                              <span class="input-group-addon" style="color:green">命令</span>
                              <input type="text" class="form-control" id="cmd" placeholder="请输入命令"/>
                              <a href="javascript:;" id="sub-cmd" class="input-group-addon btn" style="color:green" id="sub-cmd">执行</a>
                            </div>
                        </div>
                    </div>
                    <div style="height: 60px;width:74%;margin-top: 10px;display: none" id="alert">
                        <div class="alert alert-danger" >
                            <strong id="alert-msg"></strong>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 10px;">
                        <div class="col-sm-9">
                            <label>执行结果</label>
                            <pre name="" style="min-height: 580px;color: #00B83F;background-color: #313030;" class="form-control" id="txt-cmd" readonly></pre>

                        </div>
                    </div>

                </div>


                <!--批量管理文件-->
                <div class="row" style="margin-left:0px;margin-top:20px;display: none"  id="div-file">
                    <div class="row">
                        <div class="col-sm-3">
                            <div class="input-group">
                              <span class="input-group-addon"  style="color:green">文件</span>
                              <input type="file" class="form-control" style="color: #20B320;" id="up-file" >
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="input-group">

                              <input type="text" class="form-control"  id="up-file-path" value="/opt/upload/">
                              <span class="input-group-addon btn"  style="color:green" id="sub-file">上传</span>
                            </div>
                        </div>

                    </div>
                    <div style="height: 60px;width:74%;margin-top: 10px;display: none" id="alert-file">
                        <div class="alert alert-danger">
                            <strong id="alert-file-msg"></strong>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 10px;">
                        <div class="col-sm-9">
                            <label>执行结果</label>
                            <textarea name="" style="min-height: 580px;background-color: #313030;color: #00B83F" class="form-control" id="txt-file" readonly></textarea>

                        </div>
                    </div>

                </div>


                <!--批量执行脚本-->
                <div class="row" style="margin-left:0px;margin-top:20px;display: none" id="div-script">
                    <div class="row">
                        <div class="col-sm-3">
                            <div class="input-group">
                              <span class="input-group-addon"  style="color:green">脚本</span>
                              <input type="file" class="form-control" style="color: #20B320;" id="up-script" >
                                <span class="input-group-addon btn"  style="color:green" id="sub-script">执行</span>
                            </div>
                        </div>


                    </div>
                    <div style="height: 60px;width:74%;margin-top: 10px;display: none" id="alert-script">
                        <div class="alert alert-danger" >
                            <strong id="alert-script-msg"></strong>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 10px;">
                        <div class="col-sm-9">
                            <label>执行结果</label>
                            <textarea name="" style="min-height: 580px;background-color: #313030;color: #00B83F" class="form-control" id="txt-script" readonly></textarea>

                        </div>
                    </div>

                </div>



                <!--批量管理计划任务-->
                <div class="row" style="margin-left:0px;margin-top:20px;display: none" id="div-crontab">
                    <div class="row">

                        <div class="col-sm-6" style="display: inline-flex">
                            <div class="input-group" style="width: 16%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">分</span>
                              <input type="text" class="form-control"  style="color: #20B320;"  placeholder="0~59" id="Minute">
                            </div>

                            <div class="input-group" style="width: 16%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">时</span>
                              <input type="text" class="form-control"  style="color: #20B320;"  placeholder="0~23" id="Hour">
                            </div>

                            <div class="input-group" style="width: 16%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">日</span>
                              <input type="text" class="form-control"  style="color: #20B320;"  placeholder="0~31" id="Day">
                            </div>

                            <div class="input-group" style="width: 16%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">月</span>
                              <input type="text" class="form-control"  style="color: #20B320;"  placeholder="1~12" id="Month">
                            </div>

                            <div class="input-group" style="width: 16%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">周</span>
                              <input type="text" class="form-control"  style="color: #20B320;"  placeholder="0~6" id="Week">
                            </div>

                            <div class="input-group" style="width: 20%;margin-right: 5px;">
                              <span class="input-group-addon"  style="color:green">用户</span>
                                <select  class="form-control"  id="remote-user">
                                    {% for user in remote_user_list %}
                                    <option value="{{ user }}">{{ user }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="col-sm-4">
                            <div class="input-group">
                              <span class="input-group-addon"  style="color:green">命令</span>
                              <input type="text" class="form-control" style="color: #20B320;" id="cron-cmd" placeholder="输入执行命令">
                                <span class="input-group-btn">
                                    <button class="btn  btn-primary" type="button" style="border-radius: 0px;" id="add-cron">添加</button>
                                </span>

                                <span class="input-group-btn">
                                    <button class="btn  btn-warning" type="button" style="border-radius: 0px;" id="edit-cron">修改</button>
                                </span>

                                <span class="input-group-btn">
                                    <button class="btn btn-success" type="button" id="check-cron" >查询</button>
                                </span>
                            </div>
                        </div>


                    </div>
                    <div style="height: 60px;width:74%;margin-top: 10px;display: none" id="alert-cron">
                        <div class="alert alert-danger" >
                            <strong id="alert-cron-msg"></strong>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 10px;">
                        <div class="col-sm-9" style="max-height: 605px;overflow: auto;padding-top: 25px;" id="txt-cron">

                            <textarea style="min-height: 580px;background-color: #313030;color: #00B83F;" class="form-control"  readonly></textarea>
                        </div>
                        <div class="col-sm-3">
                             <label>操作提示</label>
                            <textarea style="min-height: 580px;background-color: #313030;color: #00B83F" class="form-control"  readonly>

还可以使用以下特殊字符：

星号（*）：代表所有可能的值，例如month字段如果是星号，则表示在满足其它字段的制约条件后每月都执行该命令操作

逗号（,）：可以用逗号隔开的值指定一个列表范围，例如，“1,2,5,7,8,9”

中杠（-）：可以用整数之间的中杠表示一个整数范围，例如“2-6”表示“2,3,4,5,6”

正斜线（/）：可以用正斜线指定时间的间隔频率，例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用，例如*/10，如果用在minute字段，表示每十分钟执行一次
                            </textarea>
                        </div>
                    </div>

                </div>

            </div>

        </div>

  </div>
</div>


{% endblock %}


<!-- Modal -->
{% block modal %}


{% endblock  %}

{% block js %}
<script type="text/javascript" src="/static/ztree3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/ztree3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="/static/ztree3/js/jquery.ztree.exedit.js"></script>
<script type="text/javascript" src="/static/js/sys.js"></script>
<script type="text/javascript" >



var setting = {
    view: {
        selectedMulti: false,
    },
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    edit: {
        enable: false
    }
};


var zNodes = {{ data |safe }};

$(document).ready(function(){
    treeObj=$.fn.zTree.init($("#treeDemo"), setting, zNodes);

});


function isValidIP(ip)
        {
            var reg =  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
            return reg.test(ip);
        }

//批量执行命令，提交命令
$("#sub-cmd").click(function () {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");

    var nodes=treeObj.getCheckedNodes(true);

    var node_id_list =new Array();

    for(var i=0;i<nodes.length;i++){
        var ip=nodes[i].name;
        if(isValidIP(ip))  {
            node_id_list.push(ip);
        }

    }

    var node_id_json = JSON.stringify(node_id_list);
    var cmd=$("#cmd").val();

    if (node_id_list.length>0){
        if(cmd){

            $.post("/sys/cmdrun/",{"ip_list":node_id_json,"cmd":cmd},function (data) {

                $("#txt-cmd").text(data);
            });
            msg="";
        }else{
            var msg="请输入命令！"
        }

    }else {
        var msg='请选择服务器！'
    }
    if(msg) {
        $("#alert-msg").empty();
        $("#alert-msg").append(msg);
        $("#alert").show();
    }else{
        $("#alert").hide();
    }

});


//批量上传文件
$("#sub-file").click(function () {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    var nodes=treeObj.getCheckedNodes(true);
    var node_id_list =new Array();
    for(var i=0;i<nodes.length;i++){
        var ip=nodes[i].name;
        if(isValidIP(ip))  {
            node_id_list.push(ip);
        }
    }
    var node_id_json = JSON.stringify(node_id_list);

    var upload_path = $("#up-file-path").val();

    var formData = new FormData();

    formData.append("upload_path", upload_path);
    formData.append("ip_list", node_id_json);
    upfile = $("#up-file").get(0).files[0];

    if (node_id_list.length>0){
        if(upfile){
            formData.append("upfile",upfile);
            $.ajax({
                 url: "/sys/uploadfile/",
                 type: "POST",
                 processData: false,
                 contentType: false,
                 data: formData,
                 success: function(data) {
                    $("#txt-file").text(data);
                 }
            });
            msg="";
        }else{
            msg="请选择文件！"
        }

    }else{
        msg="请选择服务器！"
    }
    if(msg) {
        $("#alert-file-msg").empty();
        $("#alert-file-msg").append(msg);
        $("#alert-file").show();
    }else{
        $("#alert-file").hide();
    }
});




//批量执行脚本
$("#sub-script").click(function () {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    var nodes=treeObj.getCheckedNodes(true);
    var node_id_list =new Array();
    for(var i=0;i<nodes.length;i++){
        var ip=nodes[i].name;
        if(isValidIP(ip))  {
            node_id_list.push(ip);
        }
    }
    var node_id_json = JSON.stringify(node_id_list);

    var formData = new FormData();

    formData.append("ip_list", node_id_json);

    script_file = $("#up-script").get(0).files[0];
    if (node_id_list.length>0){
        if(script_file){
            formData.append("script_file",script_file);
            $.ajax({
                 url: "/sys/script/",
                 type: "POST",
                 processData: false,
                 contentType: false,
                 data: formData,
                 success: function(data) {
                    $("#txt-script").text(data);
                 }
            });
            msg="";
        }else{
            msg="请上传脚本！"
        }

    }else{
        msg="请选择服务器！"
    }
    if(msg) {
        $("#alert-script-msg").empty();
        $("#alert-script-msg").append(msg);
        $("#alert-script").show();
    }else{
        $("#alert-script").hide();
    }
});




$("#nav-change").children().click(function () {
    $(this).addClass('active');
    $(this).siblings().removeClass('active');
    var title = $(this).children().html();
    if(title=='执行命令'){
        $("#div-cmd").show();
        $("#div-file").hide();
        $("#div-script").hide();
        $("#div-crontab").hide();
    }else if(title=='文件管理'){
        $("#div-cmd").hide();
        $("#div-file").show();
        $("#div-script").hide();
        $("#div-crontab").hide();
    }
    else if(title=='计划任务'){
        $("#div-cmd").hide();
        $("#div-file").hide();
        $("#div-script").hide();
        $("#div-crontab").show();
    }
    else{
        $("#div-cmd").hide();
        $("#div-file").hide();
        $("#div-script").show();
        $("#div-crontab").hide();
    }


});


//查询计划任务
$("#check-cron").click(function () {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    var nodes=treeObj.getCheckedNodes(true);
    var node_id_list =new Array();
    for(var i=0;i<nodes.length;i++){
        var ip=nodes[i].name;
        if(isValidIP(ip))  {
            node_id_list.push(ip);
        }
    }
    var node_id_json = JSON.stringify(node_id_list);


    var remote_user = $("#remote-user").val();

    if (node_id_list.length>0){

        $.get("/sys/cron/", {"ip_list":node_id_json,'remote_user':remote_user}, function(data) {
                $("#txt-cron").empty();
                $("#txt-cron").append(data);

        });
        msg="";

    }else{
        msg="请选择服务器！"
    }
    if(msg) {
        $("#alert-cron-msg").empty();
        $("#alert-cron-msg").append(msg);
        $("#alert-cron").show();
    }else{
        $("#alert-cron").hide();
    }

});


//添加计划任务
$("#add-cron").click(function () {
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    var nodes=treeObj.getCheckedNodes(true);
    var node_id_list =new Array();
    for(var i=0;i<nodes.length;i++){
        var ip=nodes[i].name;
        if(isValidIP(ip))  {
            node_id_list.push(ip);
        }
    }
    var node_id_json = JSON.stringify(node_id_list);


    var remote_user = $("#remote-user").val();
    var Minute = $("#Minute").val();
    var Hour = $("#Hour").val();
    var Day = $("#Day").val();
    var Month = $("#Month").val();
    var Week = $("#Week").val();
    var cron_cmd = $("#cron-cmd").val();

    if (node_id_list.length>0){

        if ((Minute!='' || Hour!='' || Day!=''|| Month!='' || Week!='')&& cron_cmd!=''){


            $.post("/sys/cron/", {"ip_list":node_id_json,'remote_user':remote_user,'Minute':Minute,
                'Hour':Hour,'Day':Day,'Month':Month,'Week':Week,'cron_cmd':cron_cmd
            }, function(data) {
                    $("#txt-cron").empty();
                    html_str = "<textarea name=\"\" style=\"min-height: 580px;background-color: #313030;color: #00B83F\" class=\"form-control\" id=\"txt-file\" readonly>"+data+"</textarea>"
                    $("#txt-cron").append(html_str);

            });
            msg="";
         }else{
            msg="至少填写一条时间信息,且命令不能为空!"
        }

    }else{
        msg="请选择服务器！"
    }
    if(msg) {
        $("#alert-cron-msg").empty();
        $("#alert-cron-msg").append(msg);
        $("#alert-cron").show();
    }else{
        $("#alert-cron").hide();
    }

});



$(function () {
      $('[data-toggle="tooltip"]').tooltip()
    });



</script>

{% endblock %}


